package com.iceframework.core.utils;

import java.io.UnsupportedEncodingException;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.Map.Entry;
import java.util.Properties;

import javax.activation.DataHandler;
import javax.activation.FileDataSource;
import javax.mail.BodyPart;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Multipart;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart;
import javax.mail.internet.MimeUtility;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

public class SendMailUtils {
	
	protected final Log logger = LogFactory.getLog(SendMailUtils.class);

	// smpt服务
	public String smtpServer;
	// smtp端口号
	public int smtpPort;
	// 用户名
	public String smtpUsername;
	// 密码
	public String smtpPassword;
	// 发件人昵称
	public String senderNick;
	
	private Properties props; // 系统属性 
    private Session session; // 邮件会话对象 
    private MimeMessage mimeMsg; // MIME邮件对象 
    private Multipart mp;   // Multipart对象,邮件内容,标题,附件等内容均添加到其中后再生成MimeMessage对象 


	public SendMailUtils(String smtpServer, int smtpPort, String smtpUsername, String smtpPassword, String senderNick) {
		this.smtpServer = smtpServer;
		this.smtpPort = smtpPort;
		this.smtpUsername = smtpUsername;
		this.smtpPassword = smtpPassword;
		
    	props = System.getProperties();
        props.put("mail.smtp.auth", "true");
        props.put("mail.transport.protocol", "smtp");
        props.put("mail.smtp.host", this.smtpServer);
        props.put("mail.smtp.port", this.smtpPort);
        props.put("username", this.smtpUsername);
        props.put("password", this.smtpPassword);
        // 非企业邮箱无需加
        props.put("mail.smtp.ssl.enable", "true");
        this.senderNick = senderNick;
        // 建立会话
        session = Session.getDefaultInstance(props);
        session.setDebug(false);
	}


    /**
     * 发送邮件
     * @param from 发件人
     * @param to 收件人
     * @param copyto 抄送
     * @param subject 主题
     * @param content 内容
     * @param fileList 附件列表
     * @return
     */
    public boolean sendMail(String from, String[] to, String[] copyto, String subject, String content, String[] fileList) {
        boolean success = true;
        try {
            mimeMsg = new MimeMessage(session);
            mp = new MimeMultipart(); 

            // 自定义发件人昵称
            String nick = "";
            try {
                nick = javax.mail.internet.MimeUtility.encodeText(senderNick);
            } catch (UnsupportedEncodingException e) {
                e.printStackTrace();
            }
            // 设置发件人
//          mimeMsg.setFrom(new InternetAddress(from));
            mimeMsg.setFrom(new InternetAddress(from, nick));
            // 设置收件人
            if (to != null && to.length > 0) {
                String toListStr = getMailList(to);
                mimeMsg.setRecipients(Message.RecipientType.TO, InternetAddress.parse(toListStr));
            }
            // 设置抄送人
            if (copyto != null && copyto.length > 0) {
                String ccListStr = getMailList(copyto);
                mimeMsg.setRecipients(Message.RecipientType.CC, InternetAddress.parse(ccListStr)); 
            }
            // 设置主题
            mimeMsg.setSubject(subject);
            // 设置正文
            BodyPart bp = new MimeBodyPart(); 
            bp.setContent(content, "text/html;charset=utf-8");
            mp.addBodyPart(bp);
            // 设置附件
            if (fileList != null && fileList.length > 0) {
                for (int i = 0; i < fileList.length; i++) {
                    bp = new MimeBodyPart();
                    FileDataSource fds = new FileDataSource(fileList[i]); 
                    bp.setDataHandler(new DataHandler(fds)); 
                    bp.setFileName(MimeUtility.encodeText(fds.getName(), "UTF-8", "B"));
                    mp.addBodyPart(bp); 
                }
            }
            mimeMsg.setContent(mp); 
            mimeMsg.saveChanges(); 
            // 发送邮件
            if (props.get("mail.smtp.auth").equals("true")) {
                Transport transport = session.getTransport("smtp"); 
                transport.connect((String)props.get("mail.smtp.host"), (String)props.get("username"), (String)props.get("password")); 
                transport.sendMessage(mimeMsg, mimeMsg.getAllRecipients());
                transport.close(); 
            } else {
                Transport.send(mimeMsg);
            }
            logger.debug("send mail succeed.");
        } catch (MessagingException e) {
        	logger.error("send mail failure.["+e.getMessage()+"]");
            e.printStackTrace();
            success = false;
        } catch (UnsupportedEncodingException e) {
        	logger.error("send mail failure.["+e.getMessage()+"]");
            e.printStackTrace();
            success = false;
        }
        return success;
    }
    
    /**
     * 发送邮件
     * @param to 收件人
     * @param copyto 抄送
     * @param subject 主题
     * @param content 内容
     * @param fileList 附件列表 (key:filePath,value:fileName)
     * @return
     */
    public boolean sendMail(String[] to, String[] copyto, String subject, String content, LinkedHashMap<String,String> fileMap) {
    	logger.debug("mail sending...");
    	boolean success = true;
    	try {
    		mimeMsg = new MimeMessage(session);
    		mp = new MimeMultipart(); 
    		
    		mimeMsg.setFrom(new InternetAddress(this.smtpUsername, MimeUtility.encodeText(this.senderNick)));
    		
    		// 设置收件人
    		if (to != null && to.length > 0) {
    			String toListStr = getMailList(to);
    			mimeMsg.setRecipients(Message.RecipientType.TO, InternetAddress.parse(toListStr));
    		}
    		// 设置抄送人
    		if (copyto != null && copyto.length > 0) {
    			String ccListStr = getMailList(copyto);
    			mimeMsg.setRecipients(Message.RecipientType.CC, InternetAddress.parse(ccListStr)); 
    		}
    		// 设置主题
    		mimeMsg.setSubject(subject);
    		// 设置正文
    		BodyPart bp = new MimeBodyPart(); 
    		bp.setContent(content, "text/html;charset=utf-8");
    		mp.addBodyPart(bp);
    		// 设置附件
    		if(fileMap != null && !fileMap.isEmpty()) {
    			Iterator<Entry<String, String>> iterator = fileMap.entrySet().iterator();
    			while(iterator.hasNext()) {
    				Entry<String, String> next = iterator.next();
    				bp = new MimeBodyPart();
    				FileDataSource fds = new FileDataSource(next.getKey()); 
    				bp.setDataHandler(new DataHandler(fds)); 
    				bp.setFileName(MimeUtility.encodeText(next.getValue(), "UTF-8", "B"));
    				mp.addBodyPart(bp); 
    			}
    		}
    		mimeMsg.setContent(mp); 
    		mimeMsg.saveChanges(); 
    		// 发送邮件
    		if (props.get("mail.smtp.auth").equals("true")) {
    			Transport transport = session.getTransport("smtp"); 
    			transport.connect((String)props.get("mail.smtp.host"), (String)props.get("username"), (String)props.get("password")); 
//              transport.sendMessage(mimeMsg, mimeMsg.getRecipients(Message.RecipientType.TO)); 
//              transport.sendMessage(mimeMsg, mimeMsg.getRecipients(Message.RecipientType.CC));
    			transport.sendMessage(mimeMsg, mimeMsg.getAllRecipients());
    			transport.close(); 
    		} else {
    			Transport.send(mimeMsg);
    		}
    		logger.debug("send mail succeed.");
    	} catch (MessagingException e) {
        	logger.error("send mail failure.["+e.getMessage()+"]");
    		e.printStackTrace();
    		success = false;
    	} catch (UnsupportedEncodingException e) {
        	logger.error("send mail failure.["+e.getMessage()+"]");
    		e.printStackTrace();
    		success = false;
    	}
    	return success;
    }

    /**
     * 发送邮件
     * @param from 发件人
     * @param to 收件人, 多个Email以英文逗号分隔
     * @param cc 抄送, 多个Email以英文逗号分隔
     * @param subject 主题
     * @param content 内容
     * @param fileMap 附件列表 (key:filePath,value:fileName)
     * @return
     */
    public boolean sendMail(String from, String to, String cc, String subject, String content, LinkedHashMap<String,String> fileMap) {
        boolean success = true;
        try {
            mimeMsg = new MimeMessage(session);
            mp = new MimeMultipart(); 

            // 自定义发件人昵称
            String nick = "";
            try {
                nick = MimeUtility.encodeText(senderNick);
            } catch (UnsupportedEncodingException e) {
            	logger.error("init nickname error["+e.getMessage()+"]");
            }
            // 设置发件人
            mimeMsg.setFrom(new InternetAddress(from, nick));
            // 设置收件人
            if (to != null && to.length() > 0) {
                mimeMsg.setRecipients(Message.RecipientType.TO, InternetAddress.parse(to));
            }
            // 设置抄送人
            if (cc != null && cc.length() > 0) {
                mimeMsg.setRecipients(Message.RecipientType.CC, InternetAddress.parse(cc)); 
            }
            // 设置主题
            mimeMsg.setSubject(subject);
            // 设置正文
            BodyPart bp = new MimeBodyPart(); 
            bp.setContent(content, "text/html;charset=utf-8");
            mp.addBodyPart(bp);
            // 设置附件
            if(fileMap != null && !fileMap.isEmpty()) {
    			Iterator<Entry<String, String>> iterator = fileMap.entrySet().iterator();
    			while(iterator.hasNext()) {
    				Entry<String, String> next = iterator.next();
    				bp = new MimeBodyPart();
    				FileDataSource fds = new FileDataSource(next.getKey()); 
    				bp.setDataHandler(new DataHandler(fds)); 
    				bp.setFileName(MimeUtility.encodeText(next.getValue(), "UTF-8", "B"));
    				mp.addBodyPart(bp); 
    			}
    		}
            mimeMsg.setContent(mp); 
            mimeMsg.saveChanges(); 
            // 发送邮件
            if (props.get("mail.smtp.auth").equals("true")) {
                Transport transport = session.getTransport("smtp"); 
                transport.connect((String)props.get("mail.smtp.host"), (String)props.get("username"), (String)props.get("password")); 
                transport.sendMessage(mimeMsg, mimeMsg.getAllRecipients());
                transport.close(); 
            } else {
                Transport.send(mimeMsg);
            }
            logger.debug("send mail succeed.");
        } catch (MessagingException e) {
        	logger.error("send mail failure.["+e.getMessage()+"]");
            success = false;
        } catch (UnsupportedEncodingException e) {
        	logger.error("send mail failure.["+e.getMessage()+"]");
            success = false;
        }
        return success;
    }

    public String getMailList(String[] mailArray) {
        StringBuffer toList = new StringBuffer();
        int length = mailArray.length;
        if (mailArray != null && length < 2) {
            toList.append(mailArray[0]);
        } else {
            for (int i = 0; i < length; i++) {
                toList.append(mailArray[i]);
                if (i != (length - 1)) {
                    toList.append(",");
                }

            }
        }
        return toList.toString();
    }

//	public static void main(String[] args) {
//		SendMailUtils mailUtils = new SendMailUtils("smtp.exmail.qq.com", 465, "XXX@XXX.XXX", "XXXXX","NickName");
//		String [] tos = new String [] {"XXX@qq.com"};
//        String[] copyto = null;
//        String subject = "测试一下";
//        String content = "这是邮件内容，仅仅是测试，不需要回复.";
//        LinkedHashMap<String,String> map = new LinkedHashMap<String, String>();
//        map.put("D://b.txt", "b1.txt");
//        map.put("D://a.txt", "a1.txt");
//        mailUtils.sendMail(tos, copyto, subject, content, map);
//	}

}